﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework.Input;
using Infrastructure.Managers;
using Infrastructure.ServiceInterfaces;

namespace B10_Ex02_EladLebovitch_034695114.ObjectModel
{
    public class MenuItem : Sprite 
    {
        private SpriteFont m_ScoreFont;

        public string Value
        {
            get;
            set;
        }

        public bool ShowValue
        {
            get
            {
                return (Value != string.Empty);
            }
        }

        public IInputManager InputMgr
        {
            get
            {
                return Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            }
        }

        public event EventHandler MouseOver;
        protected void OnMouseOver()
        {
            if (MouseOver != null)
            {
                MouseOver(this, new EventArgs());
            }
        }

        public event EventHandler MouseLeftClicked;
        protected void OnMouseLeftClicked()
        {
            if (MouseLeftClicked != null)
            {
                MouseLeftClicked(this, new EventArgs());
            }
        }

        public MenuItem(string i_assetName, Game i_game) 
            : base(i_assetName, i_game)
        {
            this.Value = string.Empty;
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            m_ScoreFont = ContentManager.Load<SpriteFont>(@"Fonts\ReadyFont");
        }

        public override void Update(GameTime gameTime)
        {
            Rectangle mousePos = 
                new Rectangle(
                    InputMgr.MouseState.X - this.Game.GraphicsDevice.Viewport.X,
                    InputMgr.MouseState.Y - this.Game.GraphicsDevice.Viewport.Y,
                    1,
                    1);
            if (this.Bounds.Intersects(mousePos) && InputMgr.MousePositionDelta != Vector2.Zero)
            {
                OnMouseOver();
            }

            if (this.Bounds.Intersects(mousePos) && InputMgr.ButtonPressed(eInputButtons.Left))
            {
                OnMouseLeftClicked();
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {  
            if (this.ShowValue)
            {
                this.PositionOfOrigin -= new Vector2(50, 0);

                SpriteBatch SpriteBatch = new SpriteBatch(Game.GraphicsDevice);

                SpriteBatch.Begin();
                SpriteBatch.DrawString(
                    m_ScoreFont,
                    Value,
                    this.PositionForDraw + new Vector2(this.Width, 0),
                    this.TintColor,
                    0,
                    this.PositionOrigin,
                    this.Scale,
                    SpriteEffects.None,
                    this.LayerDepth);

                SpriteBatch.End();        
            }

            base.Draw(gameTime);

            if (this.ShowValue)
            {
                this.PositionOfOrigin += new Vector2(50, 0);
            }
        }
    }
}
